package com.menker.app.di.module;

import android.app.Application;
import android.content.SharedPreferences;

//import com.facebook.stetho.okhttp3.StethoInterceptor;
import com.menker.app.Constants;
import com.menker.app.MyApp;
import com.menker.app.biz.CurrentUser;
import com.menker.app.biz.WechatShareManager;
import com.menker.app.common.DoubleClickExitDetector;
import com.menker.app.common.SharedPreferencesHelper;
import com.menker.app.common.Utils;
import com.menker.app.rest.Api;
import com.menker.app.rest.ApiHelper;
import com.menker.app.rest.CustomTrust;
import com.menker.app.rest.UnSafeHostnameVerifier;

import java.util.concurrent.TimeUnit;

import javax.inject.Singleton;

import autodagger.AutoExpose;
import dagger.Module;
import dagger.Provides;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * @author J.Huang
 * @version 1.0
 */
@Module
public class AppModule {
    private Application mApp;

    public AppModule(Application app) {
        mApp = app;
    }

//    @Provides
//    @AutoExpose(MyApp.class)
//    @Singleton
//    SharedPreferences provideSharedPreferences() {
//        String sharedPrefName = "";
//        SharedPreferences sharedPref;
//
//        if (TextUtils.isEmpty(sharedPrefName)) {
//            sharedPref = PreferenceManager.getDefaultSharedPreferences(mApp);
//        } else {
//            sharedPref = mApp.getSharedPreferences(sharedPrefName, Context.MODE_PRIVATE);
//        }
//
//        return sharedPref;
//    }

    @Provides
    @AutoExpose(MyApp.class)
    @Singleton
    SharedPreferencesHelper provideSharedPreferences() {
        return new SharedPreferencesHelper(mApp);
    }

    @Provides
    @AutoExpose(MyApp.class)
    @Singleton
    DoubleClickExitDetector provideDoubleClickExitDetector() {
        return new DoubleClickExitDetector(mApp);
    }

    @Provides
    @AutoExpose(MyApp.class)
    @Singleton
    Api provideApi(CustomTrust customTrust, UnSafeHostnameVerifier hostnameVerifier) {
        HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
        httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

        OkHttpClient.Builder httpClientBuilder = new OkHttpClient.Builder();
//                .addNetworkInterceptor(new StethoInterceptor());

        httpClientBuilder
                // 连接超时时间设置
                .connectTimeout(10, TimeUnit.SECONDS)
                .writeTimeout(10, TimeUnit.SECONDS)
                // 读取超时时间设置
                .readTimeout(30, TimeUnit.SECONDS)
                // 失败重试
                .retryOnConnectionFailure(true)
        // TODO uncomment it
        // 查看http请求内容
                .addInterceptor(httpLoggingInterceptor)
//                .addInterceptor(httpInterceptor)
        ;

        // 添加https
        httpClientBuilder
                .sslSocketFactory(customTrust.sslSocketFactory, customTrust.trustManager)
                .hostnameVerifier(hostnameVerifier);

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Constants.API.ENDPOINT)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .client(httpClientBuilder.build())
                .build();

        return retrofit.create(Api.class);
    }

    @Provides
    @AutoExpose(MyApp.class)
    @Singleton
    ApiHelper provideApiHelper(Api api, SharedPreferencesHelper sharedPrefHelper) {
        String deviceID = Utils.Device.getDeviceUniqueId(mApp);
        int version = Utils.App.getAppVersionCode();
        return new ApiHelper(api, sharedPrefHelper, deviceID, version);
    }

    @Provides
    @AutoExpose(MyApp.class)
    @Singleton
    WechatShareManager provideWechatShareManager() {
        return new WechatShareManager(mApp);
    }

    @Provides
    @AutoExpose(MyApp.class)
    @Singleton
    CurrentUser provideCurrentUser(SharedPreferencesHelper sharedPrefHelper) {
        return new CurrentUser(sharedPrefHelper);
    }

    @Provides
    @AutoExpose(MyApp.class)
    @Singleton
    UnSafeHostnameVerifier provideUnSafeHostnameVerifier() {
        return new UnSafeHostnameVerifier();
    }

    @Provides
    @AutoExpose(MyApp.class)
    @Singleton
    CustomTrust provideCustomTrust() {
        return new CustomTrust(mApp);
    }
}
